package com.hjw.service.impl;

import com.hjw.entity.Content;
import com.hjw.mapper.ContentMapper;
import com.hjw.service.IContentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hjw.vo.ContentVo;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hjw
 * @since 2025-01-16
 */
@Service
public class ContentServiceImpl extends ServiceImpl<ContentMapper, Content> implements IContentService {
    @Override
    public PageResult<Content> getPagedContent(int page, int size) {
        // 使用 Page 对象构建分页查询
        Page<Content> contentPage = this.page(new Page<>(page, size));
        return new PageResult<>(contentPage.getRecords(), contentPage.getTotal());
    }

    @Override
    public PageResult<Content> searchContent(Integer page, Integer pageSize, ContentVo searchCriteria) {
        // 如果没有搜索条件，直接返回分页内容
        if (searchCriteria == null || searchCriteria.isEmpty()) {
            return getPagedContent(page, pageSize);
        }

        // 使用 LambdaQueryWrapper 进行类型安全的查询条件构建
        LambdaQueryWrapper<Content> queryWrapper = new LambdaQueryWrapper<>();

        if (StrUtil.isNotBlank(searchCriteria.getTitle())) {
            queryWrapper.like(Content::getTitle, searchCriteria.getTitle());
        }
        if (searchCriteria.getStatus() != null) {
            queryWrapper.eq(Content::getStatus, searchCriteria.getStatus());
        }

        Page<Content> contentPage = this.page(new Page<>(page, pageSize), queryWrapper);
        return new PageResult<>(contentPage.getRecords(), contentPage.getTotal());
    }

}
